// pages/houseList/houseList.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_GETHOUSELIST} from "../../api.js";

var globleList = ['区域', '价格', '面积', '户型'];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    houseList: [],
    page: 1,
    pageSize: 10,
    label: '',
    text: '正在加载····',
    sw: true, //控制滚动到底部是否加载
    show_loading: false,
    show_no_more: false,
    show_no_data: false,
    selectList: globleList,
    show_select: false,
    selectIndex: 0,
    click: -1,
    areaList: ['不限', '孝南'],
    selectAreaIndex: -1,
    checklistPrice: [],
    priceList: ['不限', '10万以下', '10-15万', '15-20万', '20-30万', '30-40万', '40-60万', '60-100万', '100万以上'],
    priceList2: ['不限', '200以下', '200-400', '400-700', '700-1000', '1000-1500', '1500-2000', '2000-3000', '3000以上'],
    selectPriceIndex: -1,
    checklistMJ: [],
    m_j_List: ['不限', '50㎡以下', '50-70㎡', '70-90㎡', '90-110㎡', '110-130㎡', '130-150㎡', '150-200㎡', '200-300㎡', '300㎡以上'],
    selectMjIndex: -1,
    checklistHx: [],
    h_x_List: ['不限', '一室', '两室', '三室', '四室', '五室', ' 五室以上'],
    selectHxIndex: -1,
    quyu: '',
    mianji: '',
    totalPrice: '',
    huxing: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.label == 0) {
      wx.setNavigationBarTitle({
        title: '小区二手房'
      });
      this.setData({
        label: '二手房'
      })
    } else if (options.label == 1) {
      wx.setNavigationBarTitle({
        title: '小区租房'
      });
      this.setData({
        label: '租房'
      })
    } else if (options.label == 2) {
      wx.setNavigationBarTitle({
        title: '新房'
      });
      this.setData({
        label: '新房'
      })
    }
    this.getHouseList();
    this.setData({
      options: options
    });
  },

  selectClick(e) {
    // console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index
    if (this.data.click !== index) {
      this.setData({
        show_select: true,
        selectIndex: index,
        click: index
      })
    } else {
      this.setCommen()
    }

  },
  maskClick() {
    this.setCommen()
  },
  selectArea(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectAreaIndex: index
    });
    let selectResult = this.data.areaList[index];
    // console.log(selectResult)
    if (selectResult == '不限') {
      globleList[0] = "区域";
      this.setData({
        selectList: globleList,
        quyue: ''
      });
      this.getHouseList();
      this.setCommen()
    } else {
      globleList[0] = selectResult;
      this.setData({
        selectList: globleList,
        quyu: selectResult
      });
      this.getHouseList();
      this.setCommen();
    }
  },
  selectPrice(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectPriceIndex: index
    });
    let selectResult = this.data.priceList[index];
    // console.log(selectResult)
    if (selectResult == '不限') {
      globleList[1] = '价格';
      this.setData({
        selectList: globleList,
        totalPrice: ''
      });
      this.getHouseList();
      this.setCommen()
    } else {
      globleList[1] = selectResult;
      this.setData({
        selectList: globleList,
      });
      if (selectResult === '10万以下') {
        this.setData({
          totalPrice: '0-9.9'
        });
      } else if (selectResult === '100万以上') {
        this.setData({
          totalPrice: '100-1000000000'
        });
      } else {
        this.setData({
          totalPrice: selectResult.substr(0, selectResult.length - 1)
        });
      }
      this.getHouseList();
      this.setCommen()
    }
  },
  selectMj(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectMjIndex: index
    });
    let selectResult = this.data.m_j_List[index];
    // console.log(selectResult)
    if (selectResult == '不限') {
      globleList[2] = "面积";
      this.setData({
        selectList: globleList,
        mianji: ''
      });
      this.getHouseList();
      this.setCommen()
    } else {
      globleList[2] = selectResult;
      this.setData({
        selectList: globleList,
      });
      if (selectResult === '50㎡以下') {
        this.setData({
          mianji: '0-49.9'
        });
      } else if (selectResult === '300㎡以上') {
        this.setData({
          mianji: '300-10000'
        });
      } else {
        this.setData({
          mianji: selectResult.substr(0, selectResult.length - 1)
        });
      }
      this.getHouseList();
      this.setCommen()
    }
  },
  selectHx(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectHxIndex: index
    });
    let selectResult = this.data.h_x_List[index];
    if (selectResult == '不限') {
      globleList[3] = "户型";
      this.setData({
        selectList: globleList,
        huxing: ''
      });
      this.getHouseList();
      this.setCommen()
    } else {
      globleList[3] = selectResult;
      this.setData({
        selectList: globleList,
      });
      this.setCommen();
      if (selectResult === '一室') {
        this.setData({
          huxing: '1室'
        });
      } else if (selectResult === '两室') {
        this.setData({
          huxing: '2室'
        });
      } else if (selectResult === '三室') {
        this.setData({
          huxing: '3室'
        });
      } else if (selectResult === '四室') {
        this.setData({
          huxing: '4室'
        });
      } else if (selectResult === '五室') {
        this.setData({
          huxing: '5室'
        });
      } else {
        this.setData({
          huxing: '6室'
        });
      }
      this.getHouseList();
    }
  },

  setCommen() {
    this.setData({
      click: -1,
      show_select: false,
    })
  },


  getHouseList() {
    this.setData({
      page: 1
    })
    ajax.get(URI_GETHOUSELIST, {
      page: this.data.page,
      pageSize: this.data.pageSize,
      houseNew: this.data.label,
      key: '',
      quyu: this.data.quyu,
      mianji: this.data.mianji,
      totalPrice: this.data.totalPrice,
      huxing: this.data.huxing,
    }, {}, res => {
      // console.log(res.data);
      this.setData({
        houseList: res.data.data
      });
      this.setData({
        show_no_more: false,
      })
      if (this.data.houseList.length > 0 && this.data.houseList) {
        this.setData({
          show_no_data: false,
          sw: true
        })
      } else {
        this.setData({
          show_no_data: true
        })
      }
      //console.log(this.houseList);
    });
  },

  getMoreHouseList() {
    this.setData({
      show_loading: true,
    });
    ajax.get(URI_GETHOUSELIST, {
      page: this.data.page,
      pageSize: this.data.pageSize,
      houseNew: this.data.label,
      key: '',
      quyu: this.data.quyu,
      mianji: this.data.mianji,
      totalPrice: this.data.totalPrice,
      huxing: this.data.huxing,
    }, {}, res => {
      this.setData({
        houseList: this.data.houseList.concat(res.data.data),
        show_loading: false,
        sw: true
      });
      if (res.data.data.length < this.data.pageSize) {
        this.setData({
          sw: false,
          show_no_more: true
        })
      }
    });
  },

  toSearch() {
    wx.navigateTo({
      url: `/pages/search/search?label=${this.data.options.label}`
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    globleList = ['区域', '价格', '面积', '户型'];
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.sw) {
      this.setData({
        page: this.data.page + 1
      });
      this.getMoreHouseList()
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    return {
      title: this.data.options.label == 0 ? '小区二手房' : this.data.options.label == 1 ? '小区租房' : '新房',
      path: `/pages/houseList/houseList?label=${this.data.options.label}`,
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }

  }
})